package cn.rengy.web.framework.sys.userrole.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rengy.web.framework.cache.CacheService;
import cn.rengy.web.framework.constants.Constants;
import cn.rengy.web.framework.sql.DbService;
import cn.rengy.web.framework.util._Utils;
import io.jsonwebtoken.lang.Collections;
/**
 * 用户角色关系表
 *
 */
@Service
public class UserRoleService {
	@Autowired
	private DbService dbService;
	private static Logger logger = LoggerFactory.getLogger(UserRoleService.class);
	@Autowired
	private CacheService cacheService;
	/**
	 * 保存角色菜单授权
	 * @param paramMap
	 */
	@Transactional
	public void save(Map<String,Object> paramMap){
		String userId=MapUtils.getString(paramMap, "user_id");
		List<Long> curList=this.dbService.queryForSingleColumnList("select_user_role_rel_byuserid",Long.class, userId);
		List<Object> roleids=(List<Object>)paramMap.get("roleids");
		Map<String,Object> update=_Utils.getUpdate(curList, roleids);
		List<Object> insert=(List<Object>)update.get("insert");
		List<Object> delete=(List<Object>)update.get("delete");
		addRel(userId,insert);
		deleteRel(userId,delete);
		//移除缓存
		cacheService.evict(Constants.KEY_UserRole, userId);
	}
	private void deleteRel(String userId,List<Object> target) {
		if(!Collections.isEmpty(target)) {
			List<Object[]> batchArgs=new ArrayList<Object[]>(target.size());
			for(Object obj:target) {
				Object[] param=new Object[2];
				param[0]=userId;
				param[1]=obj;
				batchArgs.add(param);
			}
			this.dbService.batchUpdate("delete_sys_user_role_rel", batchArgs);
		}
	}
	private void addRel(String userId,List<Object> target) {
		if(!Collections.isEmpty(target)) {
			List<Object[]> batchArgs=new ArrayList<Object[]>(target.size());
			for(Object obj:target) {
				Object[] param=new Object[2];
				param[0]=userId;
				param[1]=obj;
				batchArgs.add(param);
			}
			this.dbService.batchUpdate("insert_sys_user_role_rel", batchArgs);
		}
	}
	
	public void addRole(String userId,Long roleId) {
		//查询是否存在
		Long c=this.dbService.querySingleColumn("select_count_user_role", Long.class, userId,roleId);
		if(c.intValue()==0) {
			this.dbService.update("insert_sys_user_role_rel", userId,roleId);
		}
	}
	/**
	 * 删除角色
	 * @param userId
	 * @param roleId
	 */
	public void deleteRole(String userId,Long roleId) {
		this.dbService.update("delete_sys_user_role_rel", userId,roleId);
	}
	public List<String> getRoles(String user_id){
		return cacheService.getLocalCache(Constants.KEY_UserRole, user_id, new Callable<List<String>>() {
	        @Override
	        public List<String> call() throws Exception {
	        	return dbService.queryForSingleColumnList("select_roles_by_userid", String.class,user_id);
	        }
	    });
	}
}
